package com.cpi.cornpp.futurepurchase.converter;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.customer.view.CustomerForecastView;
import com.cpi.cornpp.customerpurchase.view.CustomerPurchaseView;
import com.cpi.cornpp.futurepurchase.dto.FuturePurchaseDTO;
import com.cpi.cornpp.futurepurchase.dto.FuturePurchaseRealizedDTO;
import com.cpi.cornpp.futurepurchase.dto.FuturePurchaseSearchCriteriaDTO;
import com.cpi.cornpp.futurepurchase.exception.FuturePurchaseException;
import com.cpi.cornpp.futurepurchase.view.FuturePurchaseByCustomerView;
import com.cpi.cornpp.futurepurchase.view.FuturePurchaseSearchCriteriaView;
import com.cpi.cornpp.futurepurchase.view.FuturePurchaseView;
import com.cpi.cornpp.instructions.futures.view.InstructionsFuturesView;
import com.cpi.cornpp.product.view.ProductView;
import com.cpi.cornpp.productpurchase.view.ProductPurchaseView;
import com.cs.common.init.SystemParameterCollection;
import com.cs.common.init.beans.SystemParameterBean;
import com.cs.common.utils.ArrayListNoRepeat;
import com.cs.common.utils.NumberUtil;
import com.cs.common.utils.TimeUtils;

public final class FuturePurchaseConverter {

	private static final String SPANISH_FORMAT = "dd/MM/yyyy";
	/** Codigo de parametro del future type fixed*/
	private static String FUTURE_TYPE_FIXED = "FUTURE_TYPE_FIXED";
	
	private String getFixedCode(){
		String fixedType = null;
		SystemParameterCollection systemParameterCollection = null;
		SystemParameterBean systemParameterBean = null;	
		systemParameterCollection = SystemParameterCollection.getInstance();
		systemParameterBean = (SystemParameterBean)systemParameterCollection.get(FUTURE_TYPE_FIXED);
		fixedType = systemParameterBean.getValue();
		return fixedType;
	}

	public FuturePurchaseDTO convert(FuturePurchaseView view){
		FuturePurchaseDTO dto = new FuturePurchaseDTO();
		dto.setCornFutureId(view.getFuturePurchaseId());
		dto.setApplicationFuturePeriodId(view.getAccountFuturePeriodId());
		dto.setAccountFuturePeriodId(view.getApplicationFuturePeriodId());
		dto.setQuantity(view.getQuantity());
		dto.setReference(view.getReference());
		dto.setCornOrigin(view.getCornOrigin());
		dto.setUnitPrice(String.valueOf(view.getUnitPrice()));
		dto.setPurchaseDate((view.getPurchaseDate() != null) ? TimeUtils.convertStringToSQLDate(view.getPurchaseDate()) : null);
		
		return dto; 
	}
	
	public static FuturePurchaseView convert(FuturePurchaseDTO dto){
		double formattedPrice = 0.0D;
		FuturePurchaseView view = null;
		String formatedQuantity = null;
		
		view = new FuturePurchaseView();
		
		view.setFuturePurchaseId(dto.getCornFutureId());
		view.setAccountFuturePeriodId(dto.getAccountFuturePeriodId());
		view.setAccountFuturePeriodName(dto.getAccountFuturePeriodName());
		view.setApplicationFuturePeriodId(dto.getApplicationFuturePeriodId());
		view.setApplicationFuturePeriodName(dto.getApplicationFuturePeriodName());
		view.setQuantity(dto.getQuantity());
		view.setReference(dto.getReference());
		view.setUnitPrice((dto.getUnitPrice() != null) ? Double.parseDouble(dto.getUnitPrice()) : 0.0D);
		view.setCornOrigin(dto.getCornOrigin());
		if (dto.getPurchaseDate() != null){			
			view.setPurchaseDate(TimeUtils.convertSQLDateToString(dto.getPurchaseDate()));
		}
		
		formattedPrice = dto.getPrice();
		view.setPrice(formattedPrice);
		
		view.setPeriodDescription(dto.getPeriodDescription());
		view.setAvailables(dto.getAvailables().doubleValue()+dto.getQuantityAssigned());
		if( dto.isFutureAssigned() ){
			formatedQuantity = NumberUtil.convertInteger((int)dto.getQuantityAssigned());
			view.setQuantityAssgined(formatedQuantity);
		}else{
			view.setQuantityAssgined(StringUtils.EMPTY);
		}
		view.setFutureAssigned(dto.isFutureAssigned());
		return view; 
	}

	public static Collection convertDTOs(Collection listFuturePurchases){
		List arrayResult = null;
		arrayResult =  new ArrayListNoRepeat(0);
		Iterator iterator = listFuturePurchases.iterator();
		FuturePurchaseDTO dto = null;
		while(iterator.hasNext()){
			dto = (FuturePurchaseDTO)iterator.next();
			arrayResult.add(convert(dto));
		}
		
		return arrayResult;
	}
	
	
	public FuturePurchaseSearchCriteriaDTO convert(FuturePurchaseSearchCriteriaView criteriaView){
		FuturePurchaseSearchCriteriaDTO criteriaDTO = null;
		criteriaDTO = new FuturePurchaseSearchCriteriaDTO();
		criteriaDTO.setBeginPeriodYear(criteriaView.getBeginPeriodYear());
		criteriaDTO.setEndPeriodYear(criteriaView.getEndPeriodYear());
		criteriaDTO.setClientDescription(criteriaView.getClientDescription());
		criteriaDTO.setProductDescription(criteriaView.getProductDescription());
		criteriaDTO.setWithoutClient(criteriaView.isWithoutClient());
		criteriaDTO.setWithoutProduct(criteriaView.isWithoutProduct());
		return criteriaDTO;		
	}

	public FuturePurchaseByCustomerView convert(FuturePurchaseDTO futurePurchaseDTO, List customerPurchaseViews, List productPurchaseViews) {
		FuturePurchaseByCustomerView futurePurchaseByCustomerView = null;
		Date purchaseDate = null;
		Iterator customerIterator = null;
		Iterator productIterator = null;
		CustomerPurchaseView customerPurchaseView = null;
		String dealNumber = null;
		String fixedType = null;
		List productViews = null;
		ProductView productView = null;
		ProductPurchaseView productPurchaseView = null;
		
		fixedType = getFixedCode();
		futurePurchaseByCustomerView = new FuturePurchaseByCustomerView();
		futurePurchaseByCustomerView.setFuturePurchaseId(futurePurchaseDTO.getCornFutureId());
		futurePurchaseByCustomerView.setAccountPeriod(futurePurchaseDTO.getAccountFuturePeriodId());
		futurePurchaseByCustomerView.setApplicationPeriod(futurePurchaseDTO.getApplicationFuturePeriodId());
		futurePurchaseByCustomerView.setReference(futurePurchaseDTO.getReference());
		futurePurchaseByCustomerView.setPrice(NumberUtil.convertPrice(NumberUtil.parseDouble(futurePurchaseDTO.getUnitPrice())));
		futurePurchaseByCustomerView.setDealTotalNumber(NumberUtil.convertInteger(futurePurchaseDTO.getQuantity()));
		futurePurchaseByCustomerView.setCornOrigin(futurePurchaseDTO.getCornOrigin());
		purchaseDate = futurePurchaseDTO.getPurchaseDate();
		futurePurchaseByCustomerView.setDate(TimeUtils.convertJavaDateToStringDate(purchaseDate, SPANISH_FORMAT));
		productViews = new ArrayList(0);
		
		customerIterator = customerPurchaseViews.iterator();
		if(customerIterator.hasNext()){
			customerPurchaseView = (CustomerPurchaseView)customerIterator.next();
			futurePurchaseByCustomerView.setFutureDistributionId(customerPurchaseView.getFutureDistributionId());
			futurePurchaseByCustomerView.setCustomerCode(customerPurchaseView.getCustomerId());
			futurePurchaseByCustomerView.setDescription(customerPurchaseView.getDescription());
			futurePurchaseByCustomerView.setDealType(customerPurchaseView.getFutureTypeId().trim());
			if( customerPurchaseView.getFutureTypeId().trim().equals(fixedType) ){
				dealNumber = NumberUtil.convertInteger(customerPurchaseView.getQuantityFixed());
			}else{
				dealNumber = NumberUtil.convertInteger(customerPurchaseView.getQuantityDirect());
			}
			futurePurchaseByCustomerView.setDealLastNumber(dealNumber);
			futurePurchaseByCustomerView.setDealNumber(dealNumber);
			
			productIterator = productPurchaseViews.iterator();
			while( productIterator.hasNext()){
				productPurchaseView = (ProductPurchaseView)productIterator.next();
				productView = new ProductView();
				productView.setDescription(productPurchaseView.getDescription());
				productView.setProductId(productPurchaseView.getProductId());
				productView.setTon(NumberUtil.convertTon(productPurchaseView.getQuantity()));
				productViews.add(productView);
			}
			
			futurePurchaseByCustomerView.setListProducts(productViews);
		}
		
		return futurePurchaseByCustomerView;
	}
	
	public FuturePurchaseView convertFutureByClientTOFuture(FuturePurchaseByCustomerView futurePurchaseByCustomerView){
		FuturePurchaseView futurePurchaseView = null;
		List customerPurchases = null;
		List productPurchaseViews = null;
		CustomerPurchaseView customerPurchase = null;
		ProductView productView = null;
		ProductPurchaseView productPurchaseView = null;
		Iterator iterator = null;
		
		futurePurchaseView = new FuturePurchaseView();
		customerPurchases = new ArrayList(0);
		productPurchaseViews = new ArrayList(0);
		iterator = futurePurchaseByCustomerView.getListProducts().iterator();
		
		customerPurchase = new CustomerPurchaseView();
		customerPurchase.setFutureTypeId(futurePurchaseByCustomerView.getDealType());
		if( customerPurchase.getFutureTypeId().trim().equals(getFixedCode()) ){
			customerPurchase.setQuantityFixed(NumberUtil.parseInt(futurePurchaseByCustomerView.getDealNumber()));
		}else{
			customerPurchase.setQuantityDirect(NumberUtil.parseInt(futurePurchaseByCustomerView.getDealNumber()));
		}
		customerPurchase.setQuantity(NumberUtil.parseInt(futurePurchaseByCustomerView.getDealNumber()));
		customerPurchase.setCustomerId(futurePurchaseByCustomerView.getCustomerCode());
		customerPurchase.setFutureDistributionId(futurePurchaseByCustomerView.getFutureDistributionId());
		while( iterator.hasNext() ){
			productView = (ProductView)iterator.next();
			productPurchaseView = new ProductPurchaseView();
			productPurchaseView.setProductId(productView.getProductId());
			productPurchaseView.setQuantity(NumberUtil.parseDouble(productView.getTon()));
			productPurchaseViews.add(productPurchaseView);
		}
		customerPurchase.setProductPurchaseViews(productPurchaseViews);
		customerPurchases.add(customerPurchase);
		futurePurchaseView.setCustomerPurchases(customerPurchases);
		futurePurchaseView.setDefaultCustomerPurchases(customerPurchases);
		futurePurchaseView.setAccountFuturePeriodId(futurePurchaseByCustomerView.getAccountPeriod());
		futurePurchaseView.setApplicationFuturePeriodId(futurePurchaseByCustomerView.getApplicationPeriod());
		futurePurchaseView.setUnitPrice(NumberUtil.parseDouble(futurePurchaseByCustomerView.getPrice()));
		futurePurchaseView.setPurchaseDate(futurePurchaseByCustomerView.getDate());
		futurePurchaseView.setQuantity(( NumberUtil.parseInt(futurePurchaseByCustomerView.getDealTotalNumber()) - NumberUtil.parseInt(futurePurchaseByCustomerView.getDealLastNumber()) ) +  NumberUtil.parseInt(futurePurchaseByCustomerView.getDealNumber()));
		futurePurchaseView.setReference(futurePurchaseByCustomerView.getReference());
		futurePurchaseView.setFuturePurchaseId(futurePurchaseByCustomerView.getFuturePurchaseId());
		futurePurchaseView.setCornOrigin(futurePurchaseByCustomerView.getCornOrigin());
		return futurePurchaseView;
	}
	
	public FuturePurchaseView convertRealizedFutureToFutureView(Collection customerForecastViews, double contractsValue, InstructionsFuturesView instructionsFuturesView)throws FuturePurchaseException{
		FuturePurchaseView futurePurchaseView = null;
		List customerPurchases = null;
		List productPurchaseViews = null;
		CustomerPurchaseView customerPurchase = null;
		ProductPurchaseView productPurchaseView = null;
		Iterator iterator = customerForecastViews.iterator();
		CustomerForecastView customerForecastView = null;
		Calendar today;
		try{
			futurePurchaseView = new FuturePurchaseView();
			customerPurchases = new ArrayList(0);
			today = Calendar.getInstance();
			
			while( iterator.hasNext() ){
				productPurchaseViews = new ArrayList(0);
				customerForecastView = (CustomerForecastView)iterator.next();
				customerPurchase = new CustomerPurchaseView();
				productPurchaseView = new ProductPurchaseView();
				
				customerPurchase.setFutureTypeId(instructionsFuturesView.getTypeReception());
				if( customerPurchase.getFutureTypeId().trim().equals(getFixedCode()) ){
					customerPurchase.setQuantityFixed(customerForecastView.getContracts());
				}else{
					customerPurchase.setQuantityDirect(customerForecastView.getContracts());
				}
				customerPurchase.setQuantity(customerForecastView.getContracts());
				customerPurchase.setCustomerId(customerForecastView.getCustomerId());
				
				productPurchaseView.setProductId(customerForecastView.getItemId());
				if( customerForecastView.getRemainingTons() < (customerForecastView.getContracts()/contractsValue)){
					productPurchaseView.setQuantity(customerForecastView.getRemainingTons());
				}else{
					productPurchaseView.setQuantity(customerForecastView.getContracts()/contractsValue);
				}
				productPurchaseViews.add(productPurchaseView);
				customerPurchase.setProductPurchaseViews(productPurchaseViews);
				customerPurchases.add(customerPurchase);
			}
			futurePurchaseView.setCustomerPurchases(customerPurchases);
			futurePurchaseView.setAccountFuturePeriodId(instructionsFuturesView.getAccountFuturePeriodId());
			futurePurchaseView.setApplicationFuturePeriodId(instructionsFuturesView.getApplicationFuturePeriodId());
			futurePurchaseView.setUnitPrice(NumberUtil.parseDouble(instructionsFuturesView.getPriceReception()));
			futurePurchaseView.setPurchaseDate(TimeUtils.convertJavaDateToString(today.getTime()));
			futurePurchaseView.setQuantity(instructionsFuturesView.getQuantity());
			futurePurchaseView.setReference(instructionsFuturesView.getReferenceReception());
		}catch(Exception exception){
			FuturePurchaseException futurePurchaseException=null;
			exception.printStackTrace(System.out);
			futurePurchaseException= new FuturePurchaseException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_INSERT);
			throw futurePurchaseException;
		}
		return futurePurchaseView;
	}
	
	public FuturePurchaseRealizedDTO convertFuturePurchaseRealized(int cornFutureId, int purchaseInstructionFutureId, String userId){
		FuturePurchaseRealizedDTO futurePurchaseRealizedDTO = null;
		futurePurchaseRealizedDTO = new FuturePurchaseRealizedDTO();
		futurePurchaseRealizedDTO.setCornFutureId(cornFutureId);
		futurePurchaseRealizedDTO.setPurchaseInstructionFutureId(purchaseInstructionFutureId);
		futurePurchaseRealizedDTO.setUserId(userId);
		return futurePurchaseRealizedDTO;
	}
}